package com.expert.miniapp.service.impl;

import com.expert.miniapp.dao.OtherResource;
import com.expert.miniapp.dao.OtherResourceMapper;
import com.expert.miniapp.service.OtherResourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 其他资源业务逻辑实现类
 */
@Service
public class OtherResourceServiceImpl implements OtherResourceService {

    @Autowired
    private OtherResourceMapper otherResourceMapper;

    @Override
    public OtherResource getResourceById(String resourceId) {
        if (resourceId == null || resourceId.trim().isEmpty()) {
            return null;
        }
        return otherResourceMapper.findById(resourceId);
    }

    @Override
    public PageInfo<OtherResource> getAllResources(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<OtherResource> resources = otherResourceMapper.findAll();
        return new PageInfo<>(resources);
    }

    @Override
    public PageInfo<OtherResource> getRecommendedResources(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<OtherResource> resources = otherResourceMapper.findByWeightOrder();
        return new PageInfo<>(resources);
    }

    @Override
    public PageInfo<OtherResource> searchResources(String keyword, Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<OtherResource> resources = otherResourceMapper.searchByKeyword(keyword);
        return new PageInfo<>(resources);
    }
}